package com.py.rejected.service.impl;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

import com.py.rejected.domain.PyAttachment;
import com.py.rejected.domain.SysUser;
import com.py.rejected.mapper.OdSaleRejectedDetailsMapper;
import com.py.rejected.mapper.PyAttachmentMapper;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Map;

import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.py.rejected.domain.OdSaleRejectedDetails;
import com.py.rejected.mapper.OdSaleRejectedMapper;
import com.py.rejected.domain.OdSaleRejected;
import com.py.rejected.service.IOdSaleRejectedService;

import javax.annotation.Resource;

/**
 * 销售退货Service业务层处理
 * 
 * @author 童佳
 * @date 2024-12-12
 */
@Service
public class OdSaleRejectedServiceImpl implements IOdSaleRejectedService 
{
    @Resource
    private OdSaleRejectedMapper odSaleRejectedMapper;

    @Resource
    private PyAttachmentMapper pyAttachmentMapper;

    @Resource
    private OdSaleRejectedDetailsMapper odSaleRejectedDetailsMapper;

    /**
     * 查询销售退货
     * 
     * @param srId 销售退货主键
     * @return 销售退货
     */
    @Override
    public OdSaleRejected selectOdSaleRejectedBySrId(Integer srId)
    {
        OdSaleRejected odSaleRejected=odSaleRejectedMapper.selectOdSaleRejectedBySrId(srId);
        OdSaleRejectedDetails odSaleRejectedDetails=new OdSaleRejectedDetails();
        odSaleRejectedDetails.setSrCode(odSaleRejected.getSrCode());
        List<OdSaleRejectedDetails> list=odSaleRejectedMapper.selectOdSaleRejectedDetailsList(odSaleRejectedDetails);
        odSaleRejected.setOdSaleRejectedDetailsList(list);
        PyAttachment pyAttachment=new PyAttachment();
        pyAttachment.setOwnershipNumber(odSaleRejected.getSrCode());
        odSaleRejected.setFileList(Collections.singletonList(pyAttachmentMapper.selectPyAttachmentList(pyAttachment)));
        return odSaleRejected;
    }

    /**
     * 查询销售退货列表
     * 
     * @param odSaleRejected 销售退货
     * @return 销售退货
     */
    @Override
    public List<OdSaleRejected> selectOdSaleRejectedList(OdSaleRejected odSaleRejected)
    {
        return odSaleRejectedMapper.selectOdSaleRejectedList(odSaleRejected);
    }

    /**
     * 新增销售退货
     * 
     * @param odSaleRejected 销售退货
     * @return 结果
     */
    @Transactional
    @Override
    public int insertOdSaleRejected(OdSaleRejected odSaleRejected)
    {
        odSaleRejected.setIsDel("0");
        int rows = odSaleRejectedMapper.insertOdSaleRejected(odSaleRejected);
        insertOdSaleRejectedDetails(odSaleRejected);
        return rows;
    }

    /**
     * 修改销售退货
     * 
     * @param odSaleRejected 销售退货
     * @return 结果
     */
    @Transactional
    @Override
    public int updateOdSaleRejected(OdSaleRejected odSaleRejected)
    {
        List<OdSaleRejectedDetails> list=odSaleRejected.getOdSaleRejectedDetailsList();
        list.forEach(item->{
            odSaleRejectedMapper.deleteRejectedDetailsBySrCode(item.getSrCode());
        });
        insertOdSaleRejectedDetails(odSaleRejected);
        return odSaleRejectedMapper.updateOdSaleRejected(odSaleRejected);
    }

    /**
     * 批量删除销售退货
     * 
     * @param srIds 需要删除的销售退货主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOdSaleRejectedBySrIds(Integer[] srIds)
    {
        String[] values=new String[srIds.length];
        int index=0;
        for (Integer srId : srIds){
            OdSaleRejected odSaleRejected=odSaleRejectedMapper.selectOdSaleRejectedBySrId(srId);
            // 假设你想将每个订单的某个属性（例如 phCode）添加到 values 数组中
            values[index++]=odSaleRejected.getSrCode();
        }
        odSaleRejectedMapper.deleteOdSaleRejectedDetailsBySrCodes(values);
        return odSaleRejectedMapper.deleteOdSaleRejectedBySrIds(srIds);
    }

    /**
     * 删除销售退货信息
     * 
     * @param srId 销售退货主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOdSaleRejectedBySrId(Integer srId)
    {
        OdSaleRejected odSaleRejected=odSaleRejectedMapper.selectOdSaleRejectedBySrId(srId);
        odSaleRejectedMapper.deleteOdSaleRejectedDetailsBySrCode(odSaleRejected.getSrCode());
        return odSaleRejectedMapper.deleteOdSaleRejectedBySrId(srId);
    }

    /**
     * 新增${subTable.functionName}信息
     * 
     * @param odSaleRejected 销售退货对象
     */
    public void insertOdSaleRejectedDetails(OdSaleRejected odSaleRejected)
    {
        List<OdSaleRejectedDetails> odSaleRejectedDetailsList = odSaleRejected.getOdSaleRejectedDetailsList();
        String srCode = odSaleRejected.getSrCode();
        if (StringUtils.isNotNull(odSaleRejectedDetailsList))
        {
            List<OdSaleRejectedDetails> list = new ArrayList<OdSaleRejectedDetails>();
            for (OdSaleRejectedDetails odSaleRejectedDetails : odSaleRejectedDetailsList)
            {
                odSaleRejectedDetails.setSrCode(srCode);
                odSaleRejectedDetails.setRejectedPrice(odSaleRejectedDetails.getSalePrice().multiply(BigDecimal.valueOf(odSaleRejectedDetails.getRejectedNum())));
                odSaleRejectedDetails.setIsDel("0");
                list.add(odSaleRejectedDetails);
            }
            if (list.size() > 0)
            {
                odSaleRejectedMapper.batchOdSaleRejectedDetails(list);
            }
        }
    }

    @Override
    public SysUser selectBySysUserName(String userName) {
        return odSaleRejectedMapper.selectBySysUserName(userName);
    }

    @Override
    public int checkById(Map<String, Object> map) {
        return odSaleRejectedMapper.checkById(map);
    }

    @Override
    public int revokeById(Integer srId) {
        return odSaleRejectedMapper.revokeById(srId);
    }

    @Override
    public int colseById(Integer srId) {
        return odSaleRejectedMapper.colseById(srId);
    }

    @Override
    public List<OdSaleRejectedDetails> selectOdSaleRejectedDetailsList(String srCode) {
        return odSaleRejectedDetailsMapper.selectOdSaleRejectedDetailsList(srCode);
    }

    @Override
    public int selectByCode(String saleCode) {
        return odSaleRejectedMapper.selectByCode(saleCode);
    }
}
